/**
 * 模糊查询小区信息, 无返回值
 * @param {{house:string,city:string,ak:string}} params 小区名、城市名、百度ak
 * @param {Function} callback    回调函数，该函数可以接收到请求的返回值
 */
const queryHouse = function (params, callback) {
    const {house, city, ak} = params;
    const url = `http://api.map.baidu.com/place/v2/search?output=json&scope=2&q=${house}&region=${city}&ak=${ak}`;
    callback && (window.queryHouseCallback = callback);
    $.ajax({
        type: 'get',
        async: false,
        url,
        dataType: 'jsonp',
        jsonpCallback: 'queryHouseCallback',
        success: function (data) {
        }
    });
};

const queryHouseOutline = function (hid, callback) {
    const url = `http://map.baidu.com/?reqflag=pcmap&coord_type=3&from=webmap&qt=ext&ext_ver=new&l=18&uid=${hid}`;
    callback && (window.queryHouseOutlineCallback = callback);
    $.ajax({
        type: 'get',
        async: false,
        url,
        dataType: 'jsonp',
        jsonpCallback: 'queryHouseOutlineCallback',
        success: function (data) {
        }
    });
};

/**
 * 墨卡托坐标解析
 * @param {any} mercator
 * @return {{}}
 */
const parseGeo = function (mercator) {
    let s;
    if (typeof mercator != 'string') {
        return {};
    }
    const t = mercator.split('|');
    const n = parseInt(t[0]);
    const i = t[1];
    const r = t[2];
    let o = r.split(';');
    if (n === 4) {
        const a = [];
        s = 0;
        for (; s < o.length - 1; s++) {
            '1' === o[s].split('-')[0] && a.push(o[s].split('-')[1]);
        }
        o = a;
        o.push('');
    }
    let u = [];
    switch (n) {
        case 1:
            u.push(o[0]);
            break;
        case 2:
        case 3:
        case 4:
            for (s = 0; s < o.length - 1; s++) {
                let l = o[s];
                if (l.length > 100) {
                    l = l.replace(/(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*),(-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*|-?0?\.0+|0|-?[1-9]\d*)(,)/g,
                        '$1,$2;');
                    u.push(l);
                } else {
                    let c = [];
                    let d = l.split(',');
                    let f = 0;
                    for (; f < d.length; f += 2) {
                        const p = d[f];
                        const h = d[f + 1];
                        c.push(p + ',' + h);
                    }
                    u.push(c.join(';'));
                }
            }
            break;
        default:
            break;
    }

    if (u.length <= 1) {
        u = u.toString();
    }

    return {
        type: n,
        bound: i,
        points: u
    };
};

const queryHouseOfGaoDe = function (params, callback) {
    const {house, city, ak} = params;
    const url = `http://restapi.amap.com/v3/place/text?&keywords=${house}&city=${city}&output=json&offset=20&page=1&key=${ak}`;
    callback && (window.queryHouseCallback = callback);
    $.ajax({
        type: 'get',
        async: false,
        url,
        dataType: 'jsonp',
        jsonpCallback: 'queryHouseCallback',
        success: function (data) {
            console.log(data);
        }
    });
};

const drawRoad = (wd, callback) => {
    const url = `https://map.baidu.com/?qt=s&c=179&wd=${wd}`;
    callback && (window.queryRoadCallback = callback);
    $.ajax({
        type: 'get',
        async: false,
        url,
        dataType: 'jsonp',
        jsonpCallback: 'queryRoadCallback',
        success: function (data) {
        }
    });
};

const formatCommunity = (list) => {
    let newCoordinate = [];
    for (let i = 0, len = list.len; i < len; i = i + 2) {
        const lng = list[i];
        const lat = list[i + 1];
        newCoordinate.push(gcoord.transform([lng, lat], gcoord.BD09, gcoord.BD09MC));
    }
    // 社区边界
    return newCoordinate.reduce((pre, cur, index, arr) => {
        if (index === arr.length - 1) {
            return pre + cur;
        } else {
            return pre + cur + ';';
        }
    }, '');
};

const getCenterCoordinate = (points) => {
    const coordinate_X = [];
    const coordinate_Y = [];

    points.split(';').forEach(item => {
        item.split(',').forEach((coordinate, index) => {
            if (index === 0) { // x
                return coordinate_X.push(Number(coordinate));
            } else { // y
                return coordinate_Y.push(Number(coordinate));
            }
        });
    });
    const x = (Math.max(...coordinate_X) + Math.min(...coordinate_X)) / 2;
    const y = (Math.max(...coordinate_Y) + Math.min(...coordinate_Y)) / 2;
    return {
        x,
        y
    };
};

// const

const getDirections = (center, meter, gcoord) => {
    const [lng, lat] = gcoord.transform([center.x, center.y], gcoord.BD09MC, gcoord.BD09);
    const [northLng, northLat] = gcoord.transform([center.x, center.y + meter], gcoord.BD09MC, gcoord.BD09);
    const [southLng, southLat] = gcoord.transform([center.x, center.y - meter], gcoord.BD09MC, gcoord.BD09);
    const [westLng, westLat] = gcoord.transform([center.x - meter, center.y], gcoord.BD09MC, gcoord.BD09);
    const [eastLng, eastLat] = gcoord.transform([center.x + meter, center.y], gcoord.BD09MC, gcoord.BD09);
    return {
        center: new BMap.Point(lng, lat),
        north: new BMap.Point(northLng, northLat),
        south: new BMap.Point(southLng, southLat),
        west: new BMap.Point(westLng, westLat),
        east: new BMap.Point(eastLng, eastLat)
    };
};

const getPoint = (x, y, gcoord) => {
    const [lng, lat] = gcoord.transform([x, y], gcoord.BD09MC, gcoord.BD09);
    return new BMap.Point(lng, lat);
};